package com.cai.servlet.user;

import com.cai.dao.UserDao;
import com.cai.dao.impl.UserDaoImpl;
import com.cai.entity.User;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author RuoLi
 * 登录controller
 */
@WebServlet(name = "ServletLogin", value = "/login")
public class ServletLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("hello");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao userDao = new UserDaoImpl();
        User user = new User();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        user.setUser(username);
        user.setPassword(password);
        int status = userDao.userLogin(user);
        if (status != 0) {
            //登录成功！
            //获取用户信息
            User userInfo = userDao.userInfoLogin(user);
            HttpSession session = request.getSession();
            //保存用户姓名
            session.setAttribute("user", userInfo.getUser_name());
            session.setAttribute("user_name",userInfo.getUser());
            session.setAttribute("email",userInfo.getUser_email());
            session.setAttribute("password",userInfo.getPassword());
            //跳转
            response.getWriter().print(1);
        } else {
            //登录失败
            response.getWriter().print(0);
        }
    }
}
